//package com.example.demo.controller;
//
//import cn.hutool.core.bean.BeanUtil;
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.crypto.digest.DigestUtil;
//import cn.hutool.jwt.JWTUtil;
//import com.example.demo.dto.LoginDTO;
//import com.example.demo.dto.RegisterDTO;
//import com.example.demo.dto.ResponseDTO;
//import com.example.demo.exception.UserAlreadyExistsException;
//import com.example.demo.exception.ValidationException;
//import com.example.demo.mapper.UserMapper;
//import com.example.demo.model.User;
//import com.example.demo.service.AuthService;
//import com.example.demo.util.JwtTokenUtil;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.ResponseEntity;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.validation.Valid;
//import java.nio.charset.StandardCharsets;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//
//@RestController
//@RequestMapping("/api/auth")
//@Validated
//@Api(tags = "普通用户接口")
//public class AuthController {
//
//    private final AuthService authService;
//    private final AuthenticationManager authenticationManager;
//
//    /**
//     * token 秘钥字符串;
//     */
//    public static final String tokenSecret = "老鼠爱大米";
//
//    @Autowired
//    private UserMapper userMapper;
//
//    public AuthController(
//            AuthService authService,
//            AuthenticationManager authenticationManager
//    ) {
//        this.authService = authService;
//        this.authenticationManager = authenticationManager;
//    }
//
//    /**
//     * 用户注册接口
//     */
//    @PostMapping("/register")
//    @ApiOperation(value = "用户注册", notes = "注册新用户")
//    public ResponseEntity<ResponseDTO<User>> register(
//            @Valid @RequestBody RegisterDTO registerDTO
//    ) throws ValidationException, UserAlreadyExistsException {
//        User registeredUser = authService.registerUser(registerDTO);
//        return ResponseEntity.ok(ResponseDTO.success("注册成功", registeredUser));
//    }
//
//    /**
//     * 用户登录接口
//     */
//    @PostMapping("/login")
//    @ApiOperation(value = "用户登录", notes = "用户登录获取JWT Token")
//    public ResponseEntity<ResponseDTO<String>> login(
//            @Valid @RequestBody LoginDTO loginDTO  // 使用 LoginDTO
//    ) {
//
//        // 检查用户名和密码
//        String username = loginDTO.getUsername();
//        String password = loginDTO.getPassword();
//
//        User user = userMapper.findByUsername(username);
//        if (user == null) {
//            throw new RuntimeException("用户不存在");
//        }
//        String userPass = user.getPassword();
//        String encode = DigestUtil.md5Hex(password);
//        if (!StrUtil.equals(userPass, encode)) {
//            throw new RuntimeException("用户名或密码错误");
//        }
//
//        Map<String, Object> userMap = BeanUtil.beanToMap(user);
//        // 过期时间1天
//        long expire = System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1);
//        userMap.put("expire", expire);
//        String jwtToken = JWTUtil.createToken(userMap, tokenSecret.getBytes(StandardCharsets.UTF_8));
//        return ResponseEntity.ok(ResponseDTO.success("登录成功", jwtToken));
//    }
//}